package com.barneyx.mshinfoplatform.web.front.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.info.InfoEntity;
import com.barneyx.mshinfoplatform.exception.UserException;
import com.barneyx.mshinfoplatform.service.info.IInfoEntityService;
import com.barneyx.mshinfoplatform.util.InfoUtil;
import com.barneyx.mshinfoplatform.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/info/user/")
@Slf4j
public class UserInfoController {

    @Resource
    private IInfoEntityService infoEntityService;

    @Resource
    private UserUtil userUtil;

    @Resource
    private InfoUtil.Cate infoCateUtil;

    @GetMapping("page")
    public ExResult<?> Page(@RequestParam(value = "cateid", required = false, defaultValue = "-100") String cateid,
                            @RequestParam(value = "current", defaultValue = "1", required = false) Long current,
                            @RequestParam(value = "size", required = false, defaultValue = "20") Long size) throws UserException {
        Page<InfoEntity> page = infoEntityService.page(new Page<>(current, size), new LambdaQueryWrapper<InfoEntity>()
                .func(!cateid.equals("all"), c -> {
                    c.in(InfoEntity::getInfoCateId, infoCateUtil.getInfoCateChildIds(cateid).toArray());
                })
                .in(InfoEntity::getUserPlatformId,userUtil.getUserIds().toArray())
                .orderByDesc(InfoEntity::getCreateAt));
        return ExResult.ofAuto(page);
    }

}
